package com.example.plugintest;

import java.io.File;
import java.util.ArrayList;

import com.example.plugintest.dynamicload.internal.DLIntent;
import com.example.plugintest.dynamicload.internal.DLPluginManager;
import com.example.plugintest.dynamicload.utils.DLUtils;

import android.app.Activity;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemClickListener {

	public static final String FROM = "extra.from";
	public static final int FROM_INTERNAL = 0;
	public static final int FROM_EXTERNAL = 1;

	private ArrayList<PluginItem> mPluginItems = new ArrayList<PluginItem>();
	private PluginAdapter mPluginAdapter;

	private ListView mListView;
	private TextView mNoPluginTextView;

	private ServiceConnection mConnection;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
	}

	private void initView() {
		mPluginAdapter = new PluginAdapter();
		mListView = (ListView) findViewById(R.id.plugin_list);
		mNoPluginTextView = (TextView) findViewById(R.id.no_plugin);
	}

	private void initData() {
		String pluginFolder = Environment.getExternalStorageDirectory() + "/DynamicLoadHost";
		Log.d("hailongqiu", "hailongqiu pluginFolder:" + pluginFolder);
		File file = new File(pluginFolder);
		File[] plugins = file.listFiles();
		
		// 没有发现插件.
		if (plugins == null || plugins.length == 0) {
			mNoPluginTextView.setVisibility(View.VISIBLE);
			return;
		}

		// 处理目录下的插件.
		for (File plugin : plugins) {
			PluginItem item = new PluginItem();
			item.pluginPath = plugin.getAbsolutePath();
			item.packageInfo = DLUtils.getPackageInfo(this, item.pluginPath);
			if (item.packageInfo.activities != null && item.packageInfo.activities.length > 0) {
				item.launcherActivityName = item.packageInfo.activities[0].name;
			}
			if (item.packageInfo.services != null && item.packageInfo.services.length > 0) {
				item.launcherServiceName = item.packageInfo.services[0].name;
			}
			mPluginItems.add(item);
			DLPluginManager.getInstance(this).loadApk(item.pluginPath);
		}

		mListView.setAdapter(mPluginAdapter);
		mListView.setOnItemClickListener(this);
		mPluginAdapter.notifyDataSetChanged();
	}

	private class PluginAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		@Override
		public int getCount() {
			return mPluginItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mPluginItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.plugin_item, parent, false);
				holder = new ViewHolder();
				holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
				holder.appName = (TextView) convertView.findViewById(R.id.app_name);
				holder.apkName = (TextView) convertView.findViewById(R.id.apk_name);
				holder.packageName = (TextView) convertView.findViewById(R.id.package_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			PluginItem item = mPluginItems.get(position);
			PackageInfo packageInfo = item.packageInfo;
			holder.appIcon.setImageDrawable(DLUtils.getAppIcon(MainActivity.this, item.pluginPath));
			holder.appName.setText(DLUtils.getAppLabel(MainActivity.this, item.pluginPath));
			holder.apkName.setText(item.pluginPath.substring(item.pluginPath.lastIndexOf(File.separatorChar) + 1));
			holder.packageName.setText(packageInfo.applicationInfo.packageName + "\n" + item.launcherActivityName + "\n"
					+ item.launcherServiceName);
			return convertView;
		}
	}

	private static class ViewHolder {
		public ImageView appIcon;
		public TextView appName;
		public TextView apkName;
		public TextView packageName;
	}

	/**
	 * 插件资料
	 */
	public static class PluginItem {
		public PackageInfo packageInfo;
		public String pluginPath;
		public String launcherActivityName;
		public String launcherServiceName;

		public PluginItem() {
		}
	}

	// 插件列表单击
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		PluginItem item = mPluginItems.get(position);
		DLPluginManager pluginManager = DLPluginManager.getInstance(this);
		// 启动插件
		pluginManager.startPluginActivity(this, new DLIntent(item.packageInfo.packageName, item.launcherActivityName));
	}

}
